package com.imaginagroup.GoogleAPI;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class GoogleAPIActivity extends MapActivity {
	
	MapView mapView;
	MapController mapController; 
	MyLocationOverlay myLocationOverlay;
    Double lat = 37.422006*1E6;
    Double lng = -122.084095*1E6;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        LocationManager locationManager =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String bestProvider = locationManager.getBestProvider(loadCriteria(), true);
        locationManager.requestLocationUpdates(bestProvider, 1, 1, new LocListener());      

        GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
        
        mapView = (MapView) findViewById(R.id.myMapa);
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(true);
        
        final MapController mapController = mapView.getController();
        mapController.setCenter(point);
        mapController.setZoom(1);
        
        
        List<Overlay> overlays = mapView.getOverlays();
        final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
        myLocationOverlay.enableMyLocation();
        
        myLocationOverlay.runOnFirstFix(new Runnable() {
        public void run() {
        	mapController.animateTo(myLocationOverlay.getMyLocation());
        }});
        
        
        overlays.add(myLocationOverlay);
        
        
    }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	private void updateLocation (Location location) throws IOException {
	    	if (location != null) {
	    		lat = location.getLatitude();
	    		lng = location.getLongitude();	    		
	    	} 
	    	else {
	    		
	    	}
	    	
	    }
	
    private Criteria loadCriteria(){
    	Criteria criteria = new Criteria(); 
    	//Set preferences.
    	criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
    	criteria.setPowerRequirement(Criteria.POWER_LOW); 
    	criteria.setAltitudeRequired(true);
    	criteria.setBearingRequired(false);
    	criteria.setSpeedRequired(true); 
    	criteria.setCostAllowed(true);
    	
    	return criteria;
    }
    
    private class LocListener implements LocationListener {
    	@Override
    	public void onLocationChanged(Location location) {
    		try {
				updateLocation(location);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	@Override
    	public void onProviderDisabled(String provider) {
    		try {
				updateLocation(null);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	@Override
    	public void onProviderEnabled(String provider) { }
    	@Override
    	public void onStatusChanged(String provider, int status, Bundle extras) { }
    }

}